import { defineStore } from 'pinia'
import { ref } from 'vue'
import router from "@/router";

/**
 * smallWindow 小窗管理模块
 * 显示时间
 */
export const useSmallWindowStore = defineStore('smallWindow', () => {
    const showSmallWindow = ref(false)
    const clockTime = ref(0)
    const isRunning = ref(false)
    let timer = null

    const startTimer = () => {
        if (!isRunning.value) {
            timer = setInterval(() => {
                clockTime.value++
            }, 1000)
            isRunning.value = true
        }
    }

    const pauseTimer = () => {
        clearInterval(timer)
        isRunning.value = false
    }

    const handleDoubleClick = () => {
        setTimeout(() => {
            showSmallWindow.value = false
            router.push('/clock')
        }, 300)  // 先缩小动画 300ms，再跳转
    }

    const resetTimer = () => {
        clearInterval(timer)
        clockTime.value = 0
        isRunning.value = false
    }

    return {
        showSmallWindow,
        clockTime,
        isRunning,
        startTimer,
        pauseTimer,
        resetTimer,
        handleDoubleClick,
    }
})
